/**
 * Copyright (c) 2018 qar All rights reserved.
 *
 * https://www.qar.io
 *
 * 版权所有，侵权必究！
 */

package com.example.controller;

import com.example.dto.SysParamsDTO;
import com.example.feign.ParamsFeignClient;
import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.service.SysParamsService;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 参数管理
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
@RestController
@RequestMapping("params")
@Tag(name = "参数管理")
public class SysParamsController implements ParamsFeignClient {
    @Autowired
    private SysParamsService sysParamsService;

    @PostMapping("page")
    @Operation(summary = "分页")
    public Result<PageData<SysParamsDTO>> page(@RequestBody Map<String, Object> params) {
        PageData<SysParamsDTO> page = sysParamsService.page(params);
        return new Result<PageData<SysParamsDTO>>().ok(page);
    }

    @PostMapping("list")
    @Operation(summary = "列表")
    public Result<List<SysParamsDTO>> list(@RequestBody Map<String, Object> params) {
        List<SysParamsDTO> list = sysParamsService.list(params);
        return new Result<List<SysParamsDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<SysParamsDTO> get(@PathVariable("id") Long id) {
        SysParamsDTO data = sysParamsService.get(id);
        return new Result<SysParamsDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result save(@RequestBody SysParamsDTO dto) {
        ValidatorUtils.validateEntity(dto, DefaultGroup.class, AddGroup.class);
        sysParamsService.save(dto);
        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result update(@RequestBody SysParamsDTO dto) {
        ValidatorUtils.validateEntity(dto, DefaultGroup.class, UpdateGroup.class);
        sysParamsService.update(dto);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result delete(@RequestBody Long[] ids) {
        AssertUtils.isArrayEmpty(ids, "数组参数不能为空");
        sysParamsService.delete(ids);
        return Result.ok();
    }

//    @GetMapping("export")
////    @Operation(summary = "导出")
////    @LogOperation("Export Params")
//    @PreAuthorize("hasAuthority('sys:params:export')")
////    @ApiImplicitParam(name = "paramCode", value = "参数编码", paramType = "query", dataType = "String")
//    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response)
//            throws Exception {
//        List<SysParamsDTO> list = sysParamsService.list(params);
//
//        ExcelUtils.exportExcelToTarget(response, null, "参数管理", list, SysParamsExcel.class);
//    }

    /**
     * 根据参数编码，获取参数值
     * 
     * @param paramCode 参数编码
     * @return 返回参数值
     */
    @GetMapping("code/{paramCode}")
    public String getValue(@PathVariable("paramCode") String paramCode) {
        return sysParamsService.getValue(paramCode);
    }

    /**
     * 根据参数编码，更新参数值
     * 
     * @param paramCode  参数编码
     * @param paramValue 参数值
     */
    @PutMapping("code/{paramCode}")
    public void updateValueByCode(@PathVariable("paramCode") String paramCode,
            @RequestParam("paramValue") String paramValue) {
        sysParamsService.updateValueByCode(paramCode, paramValue);
    }
}